package com.egao.common.module.cms.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.Link;
import com.egao.common.module.cms.service.LinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 友情链接管理
 * @author pyx
 */
@Api(tags = "友情链接管理")
@RestController
@RequestMapping("${api}")
public class LinkController extends BaseController {
    @Autowired
    private LinkService linkService;

    @PreAuthorize("hasAuthority('cms:link:list')")
    @ApiOperation("分页查询链接")
    @OperLog(value = "链接管理", desc = "分页查询链接")
    @GetMapping("/cms/link/page")
    public PageResult<Link> page(HttpServletRequest request) {
        PageParam<Link> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(new String[]{"sort"}, new String[]{"create_time"});
        return new PageResult<>(linkService.listPage(pageParam),pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('cms:link:list')")
    @ApiOperation("查询单个链接")
    @OperLog(value = "链接管理", desc = "查询单个链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "链接id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/cms/link/get")
    public JsonResult listGetId(Integer id) {
        return linkService.getId(id);
    }

    @PreAuthorize("hasAuthority('cms:link:insert')")
    @ApiOperation("添加链接")
    @OperLog(value = "链接管理", desc = "添加链接", result = true)
    @PostMapping("/cms/link/insert")
    public JsonResult insert(@RequestBody Link link) {
        return linkService.insertLink(link);
    }

    @PreAuthorize("hasAuthority('cms:link:update')")
    @ApiOperation("修改链接")
    @OperLog(value = "链接管理", desc = "修改链接", result = true)
    @PutMapping("/cms/link/update")
    public JsonResult update(@RequestBody Link link) {
        return linkService.updateLink(link);
    }

    @PreAuthorize("hasAuthority('cms:link:delete')")
    @ApiOperation("删除链接")
    @OperLog(value = "链接管理", desc = "删除链接", result = true)
    @DeleteMapping("/cms/link/delete")
    public JsonResult delete(Integer id) {
        return linkService.deleteById(id);
    }
}
